package com.infodms.dms.actions.claim.basicData;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.mvc.context.ResponseWrapper;
import jxl.Workbook;
import jxl.write.Label;

public class ToExcel {
	/**
	 * toExcel
	 * 
	 * @param response
	 * @param request
	 * @param head
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public static Object toExcel(ResponseWrapper response,
			RequestWrapper request, String[] head, List<String[]> list)
			throws Exception {

		String name = "轎車維修車輛數.xls";
		jxl.write.WritableWorkbook wwb = null;
		OutputStream out = null;
		try {
			response.setContentType("application/octet-stream");
		    response.addHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(name, "utf-8"));
			out = response.getOutputStream();
			wwb = Workbook.createWorkbook(out);
			jxl.write.WritableSheet ws = wwb.createSheet("sheettest", 0);

			if (head != null && head.length > 0) {
				for (int i = 0; i < head.length; i++) {
					ws.addCell(new Label(i, 0, head[i]));
				}
			}
			
			for (int z = 1; z < list.size() + 1; z++) {
				String[] str = list.get(z - 1);
				for (int i = 0; i < str.length; i++) {
					ws.addCell(new Label(i, z, str[i]));
				}
			}
			wwb.write();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (null != wwb) {
				wwb.close();
			}
			if (null != out) {
				out.close();
			}
		}
		return null;
	}
	
	
	public static Object toNewExcel(ResponseWrapper response,
			RequestWrapper request, String[] head, List<String[]> list,String name)
			throws Exception {

		jxl.write.WritableWorkbook wwb = null;
		OutputStream out = null;
		try {
			response.setContentType("application/octet-stream");
		    response.addHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(name, "utf-8"));
			out = response.getOutputStream();
			wwb = Workbook.createWorkbook(out);
			jxl.write.WritableSheet ws = wwb.createSheet("sheettest", 0);

			if (head != null && head.length > 0) {
				for (int i = 0; i < head.length; i++) {
					ws.addCell(new Label(i, 0, head[i]));
				}
			}
			int counts=0;
			int flag=0;
			for (int z = 1; z < list.size() + 1; z++) {
				int pageSizess=z/30000;
				counts++;
				if(pageSizess>flag){
					counts=1;
				}
				flag=pageSizess;
				System.out.println("KKKKK:"+z);
				pageSizess=pageSizess<1?0:pageSizess;
				pageSizess=(new BigDecimal(pageSizess).setScale(0, BigDecimal.ROUND_HALF_UP)).intValue();
				String[] str = list.get(z - 1);
				for (int i = 0; i < str.length; i++) {
					ws.addCell(new Label(i, z, str[i]));
				}
			}
			wwb.write();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (null != wwb) {
				wwb.close();
			}
			if (null != out) {
				out.close();
			}
		}
		return null;
	}
	//zhumingwei add by 2011-02-23
	public static Object toExcel111(ResponseWrapper response,
			RequestWrapper request, String[] head, List<String[]> list)
			throws Exception {

		String name = "三包结算统计表.xls";
		jxl.write.WritableWorkbook wwb = null;
		OutputStream out = null;
		try {
			response.setContentType("application/octet-stream");
		    response.addHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(name, "utf-8"));
			out = response.getOutputStream();
			wwb = Workbook.createWorkbook(out);
			jxl.write.WritableSheet ws = wwb.createSheet("sheettest", 0);

			if (head != null && head.length > 0) {
				for (int i = 0; i < head.length; i++) {
					ws.addCell(new Label(i, 0, head[i]));
				}
			}
			int pageSize=list.size()/30000;
			for (int z = 1; z < list.size() + 1; z++) {
				String[] str = list.get(z - 1);
				for (int i = 0; i < str.length; i++) {
					ws.addCell(new Label(i, z, str[i]));
				}
			}
			wwb.write();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (null != wwb) {
				wwb.close();
			}
			if (null != out) {
				out.close();
			}
		}
		return null;
	}
	public static Object toExcel2(ResponseWrapper response,
			RequestWrapper request, String[] head, List list)
			throws Exception {

		String name = "轎車維修車輛數.xls";
		jxl.write.WritableWorkbook wwb = null;
		OutputStream out = null;
		try {
			response.setContentType("application/octet-stream");
		    response.addHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(name, "utf-8"));
			out = response.getOutputStream();
			wwb = Workbook.createWorkbook(out);
			jxl.write.WritableSheet[] wsData=new jxl.write.WritableSheet[20];
			
			int pageSize=list.size()/30000;
			for(int j=-1;j<pageSize;j++){
				wsData[(j+1)] = wwb.createSheet("sheettest"+(j+1), (j+1));					
			}
			if (head != null && head.length > 0) {
				for (int i = 0; i < head.length; i++) {
					wsData[0].addCell(new Label(i, 0, head[i]));
				}
			}
			
			int counts=0;
			int flag=0;
			for (int z = 1; z < list.size() + 1; z++) {
				int pageSizess=z/30000;
				counts++;
				if(pageSizess>flag){
					counts=1;
				}
				flag=pageSizess;
				System.out.println("KKKKK:"+z);
				pageSizess=pageSizess<1?0:pageSizess;
				pageSizess=(new BigDecimal(pageSizess).setScale(0, BigDecimal.ROUND_HALF_UP)).intValue();

				Map map=(Map)list.get(z-1);
				String  ROWNUM= String.valueOf(map.get("ROWNUM"));
				String VIN = String.valueOf(map.get("VIN"));
				//wsData[pageSizess].addCell(new Label(0, counts, ROWNUM));
				wsData[pageSizess].addCell(new Label(0, counts, VIN));
//				for (int i = 0; i < str.length; i++) {
//					wsData[pageSizess].addCell(new Label(i, z, str[i]));
//				}
			}
			
			wwb.write();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (null != wwb) {
				wwb.close();
			}
			if (null != out) {
				out.close();
			}
		}
		return null;
	}


}
